<?php
/**
 * Created by PhpStorm.
 * User: JING
 * Date: 2022-06-08
 * Time: 14:27
 */
namespace app\workerman;

use GatewayWorker\Lib\Gateway;
use think\worker\Application;
use Workerman\Worker;
use think\Db;
/**
 * Worker 命令行服务类
 */
class SocketEvents
{
    /**
     * onWorkerStart 事件回调
     * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
     *
     * @access public
     * @param  \Workerman\Worker    $businessWorker
     * @return void
     */
    public static function onWorkerStart(Worker $businessWorker)
    {
        $app = new Application;
        $app->initialize();
    }

    /**
     * onConnect 事件回调
     * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
     *
     * @access public
     * @param  int       $client_id
     * @return void
     */
    public static function onConnect($client_id)
    {
        $data = [
            'type'=>'connect',
            'data'=>[
                'client_id'=>$client_id,
            ]

        ];
        Gateway::sendToCurrentClient(json_encode($data));
        //Gateway::sendToCurrentClient("Your client_id is $client_id");
    }

    /**
     * onWebSocketConnect 事件回调
     * 当客户端连接上gateway完成websocket握手时触发
     *
     * @param  integer  $client_id 断开连接的客户端client_id
     * @param  mixed    $data
     * @return void
     */
    public static function onWebSocketConnect($client_id, $data)
    {
        echo "WebSocketConnect[".$client_id."]\n";
        //print_r($data);
       // echo '\n';
    }

    /**
     * onMessage 事件回调
     * 当客户端发来数据(Gateway进程收到数据)后触发
     *
     * @access public
     * @param  int       $client_id
     * @param  mixed     $data
     * @return void
     */
    public static function onMessage($client_id, $data)
    {
        $message = json_decode($data,true);
        $message_type = isset($message['type'])?$message['type']:'';
        switch($message_type) {
            case 'bind':
                $user = $message['data']['user'];
                Gateway::bindUid($client_id,$user['id']);
                Gateway::sendToUid($user['id'],json_encode([
                    'type'=>'bind',
                    'data'=>[
                        'msg'=>'绑定成功',
                        'uid'=>$user['id'],
                        'client_id'=>$client_id,
                    ],
                ]));

                return;
            case 'ping':
                Gateway::sendToCurrentClient(json_encode($message));
                return;
            case 'text':
                Gateway::sendToAll(json_encode($message));
                return;

            default:
                echo "unknown message $data" . PHP_EOL;
        }
        //Gateway::sendToAll($data);
    }

    /**
     * onClose 事件回调 当用户断开连接时触发的方法
     *
     * @param  integer $client_id 断开连接的客户端client_id
     * @return void
     */
    public static function onClose($client_id)
    {
        GateWay::sendToAll(json_encode([
            'type'=>'toall',
            'data'=>[
                'msg'=>'用户断开连接',
                'client_id'=>$client_id,
            ],
        ]));
    }

    /**
     * onWorkerStop 事件回调
     * 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
     *
     * @param  \Workerman\Worker    $businessWorker
     * @return void
     */
    public static function onWorkerStop(Worker $businessWorker)
    {
        echo "WorkerStop\n";
    }
}